#!/bin/sh
: Patch Edit.app to always instantiate NXDataLinkManager
: Eric P. Scott, San Francisco State University, August 1994
PATH=/bin:/usr/ucb:/usr/bin; export PATH
echo One moment while I check which version you have... >&2
case @`sum /NextApps/Edit.app/Edit` in
@) exit 1 ;;
@55340\ \ \ 356) EDITVER=31M; echo 3.1/m68k OK >&2 ;;
@37609\ \ \ 380) EDITVER=31I; echo 3.1/i386 OK >&2 ;;
@14017\ \ \ 356) EDITVER=32M; echo 3.2/m68k OK >&2 ;;
@60216\ \ \ 372) EDITVER=32I; echo 3.2/i386 OK >&2 ;;
@05011\ \ \ 437) EDITVER=Blaze2C; echo 3.2prerelease/hppa OK >&2 ;;
@60955\ \ \ 437) EDITVER=Blaze2X; echo 3.2hp OK >&2 ;;
@35153\ \ \ 401) 
  echo Ick.\  You\'re running 3.0.\  You need 3.1 or 3.2. >&2
  exit 1 ;;
*) echo Wrong Edit version for this procedure.\  Sorry. >&2
  exit 1 ;;
esac
if [ ! -f /bin/cc ]; then
  echo You don\'t have a C compiler.\  Can\'t continue. >&2
  exit 1
fi
echo "" >&2
echo This procedure will make a copy of the NeXT Edit application that >&2
echo will work properly with RBrowser.\  It will not alter the original >&2
echo Edit.app in any way.\  This copy will contain revised object code >&2
echo and symbolic links to the original files.\  You must destroy this >&2
echo copy if you change operating system releases, or install or >&2
echo deinstall language packages \(but you can run this script again >&2
echo to generate a fresh copy\). >&2
echo "" >&2
while :
do
  echo -n "Installation directory [~/Apps]: " >&2
  if read dir; then :; else echo >&2; exit 1; fi
  case @$dir in
  @) dir=${HOME?}/Apps ;;
  *) ;;
  esac
  if [ -d $dir ]; then
    if [ -w $dir ]; then break; fi
    echo $dir isn\'t writable >&2
  elif [ -f $dir ]; then
    echo $dir exists, but it\'s not a directory >&2
  else
    echo $dir doesn\'t exist >&2
  fi
  echo "" >&2
  echo Try something else or type Control-D to exit. >&2
done
cd $dir
case $dir in
/*) ;;
*) dir="`pwd`" ;;
esac
umask 022
mkdir Edit.app||exit 1
echo Copying files... >&2
cd Edit.app
ln -s /NextApps/Edit.app/* .
rm -f Edit
cp -p /NextApps/Edit.app/Edit .
echo Compiling binary patcher... >&2
cd /tmp
cat <<\INTELSUX >mangle$$.c
#include <stdio.h>
struct subs {
	long offset;
	int old, new;
} subs[]={
#ifdef EDITVER31M
	{ 28251L, 'g', 'N' },
	{ 28252L, '\f', 'q' },
	{ 31989L, 'g', 'N' },
	{ 31990L, '>', 'q' },
	{ 364121L, EOF, EOF }
#endif
#ifdef EDITVER31I
	{ 30572L, 't', '\220' },
	{ 30573L, '\020', '\220' },
	{ 34948L, 't', '\220' },
	{ 34949L, 'K', '\220' },
	{ 388345L, EOF, EOF }
#endif
#ifdef EDITVER32M
	{ 27439L, 'g', 'N' },
	{ 27440L, '\f', 'q' },
	{ 31461L, 'g', 'N' },
	{ 31462L, '>', 'q' },
	{ 364501L, EOF, EOF }
#endif
#ifdef EDITVER32I
	{ 30676L, 't', '\220' },
	{ 30677L, '\020', '\220' },
	{ 35104L, 't', '\220' },
	{ 35105L, 'G', '\220' },
	{ 380533L, EOF, EOF }
#endif
#ifdef EDITVERBlaze2C
	{ 46381L, 0206, '\b' },
	{ 46382L, '`', '\0' },
	{ 46383L, ' ', '\002' },
	{ 46384L, ' ', '@' },
	{ 52617L, 0206, '\b' },
	{ 52618L, '`', '\0' },
	{ 52619L, ' ', '\002' },
	{ 52620L, 0272, '@' },
	{ 447405L, EOF, EOF }
#endif
#ifdef EDITVERBlaze2X
	{ 46981L, 0206, '\b' },
	{ 46982L, '`', '\0' },
	{ 46983L, ' ', '\002' },
	{ 46984L, ' ', '@' },
	{ 53209L, 0204, '\b' },
	{ 53210L, 0240, '\0' },
	{ 53211L, ' ', '\002' },
	{ 53212L, 0272, '@' },
	{ 447465L, EOF, EOF }
#endif
};
main() {
	int c;
	long pos;
	struct subs *s=subs;

	for (pos=0L;(c=getchar())!=EOF;) {
		if (++pos==s->offset) {
			if (c!=s->old) {
				(void)fprintf(stderr, "mismatch at %ld\n",
					pos);
				exit(1);
			}
			c=s++->new;
		}
		putchar(c);
	}
	if (++pos!=s->offset) {
		(void)fputs("premature EOF\n", stderr);
		exit(1);
	}
	exit(0);
}
INTELSUX
cc -o .mangle$$ -s -object -O -bsd -DEDITVER${EDITVER?} mangle$$.c||exit 1
rm -f mangle$$.c
echo Generating patched Edit executable... >&2
cd $dir
/tmp/.mangle$$ </NextApps/Edit.app/Edit >Edit.app/Edit||rm -rf Edit.app
rm -f /tmp/.mangle$$
if [ -d Edit.app ]; then
  echo Installation complete >&2
  echo "" >&2
  echo If you have /NextApps/Edit.app in your dock, remove it. >&2
  echo You\'ll need to dock the new Edit.app unless it resides in one of >&2
  echo your ApplicationPaths folders that\'s searched ahead of /NextApps >&2
  echo \(this generally means \~/Apps or /LocalApps\).\  I suggest >&2
  echo docking it in any case. >&2
else
  echo Installation FAILED >&2
  exit 1
fi
